package swu.cx.finalproject

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.Toast
import cn.bmob.v3.BmobSMS
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.QueryListener
import cn.bmob.v3.listener.UpdateListener
import kotlinx.android.synthetic.main.activity_register.*
import kotlinx.android.synthetic.main.activity_register.*

class RegisterActivity : AppCompatActivity(),TextWatcher {
    override fun afterTextChanged(p0: Editable?) {
        register.isEnabled = (Rusername.text.isNotEmpty() && Rpassword.text.isNotEmpty() && Rpassword2.text.isNotEmpty()
                && Rpassword.text.toString() == Rpassword2.text.toString())
        if(Rpassword.text.toString() != Rpassword2.text.toString()
            && Rpassword.text.isNotEmpty()
            && Rpassword2.text.isNotEmpty()
            && Rpassword.text.length == Rpassword2.text.length){
            Tip.alpha = 1f
        }else{
            Tip.alpha = 0f
        }
    }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
        Rusername.addTextChangedListener(this)
        Rpassword.addTextChangedListener(this)
        Rpassword2.addTextChangedListener(this)

        getCode.setOnClickListener {
            BmobSMS.requestSMSCode("19112745253","cx" , object : QueryListener<Int>(){
                override fun done(p0: Int?, p1: BmobException?) {
                        if (p1 == null){
                            Toast.makeText(this@RegisterActivity,"验证码发送成功",Toast.LENGTH_LONG).show()
                        }else{
                            Toast.makeText(this@RegisterActivity,"验证码发送失败",Toast.LENGTH_LONG).show()
                        }
                }
            })
        }//点击请求发送验证码

        register.setOnClickListener {
            BmobSMS.verifySmsCode("19112745253",Code.text.toString(),object:UpdateListener(){
                override fun done(p0: BmobException?) {
                    if (p0 == null){
                        //验证码正确
               SharedPreferenceUtil.getInstance(this@RegisterActivity).SaveUserInfo(Rusername.text.toString())
               SharedPreferenceUtil.getInstance(this@RegisterActivity).SavePasswordInfo(Rpassword.text.toString())
                        Intent(this@RegisterActivity,LoginActivity::class.java).apply {
                            startActivity(this)
                            Toast.makeText(this@RegisterActivity,"注册成功",Toast.LENGTH_LONG).show()
                        }//跳转到登录界面
                    }else{
                        Toast.makeText(this@RegisterActivity,"验证码错误",Toast.LENGTH_LONG).show()
                    }
                }
            })
        }//检验验证码，跳转到登录界面

    }
}
